package com.ddstation.ncovreport.controller;

import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.drug.service.MaInvsDrugService;
import com.ddstation.ncovreport.model.NcovParModel;
import com.ddstation.ncovreport.model.NocResModel;
import com.ddstation.ncovreport.model.NocStatisticsResModel;
import com.ddstation.ncovreport.service.NcovReportService;
import com.ddstation.sewage.service.SewageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 新冠新增功能controller
 *
 * @author fubingbing
 * @date 2020年2月18日
 */
@Controller
public class DtNcovReportController {

    @Inject
    private NcovReportService ncovReportService;
    @Inject
    private SewageService sewageService;

    @Resource
    private MaInvsDrugService maInvsDrugService;

    /**
     * 疑似冠状结果统计跳转页面
     *
     * @param
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "invs_ncov_report.do")
    public String ncovReportInitEvent(
            Model model,
            HttpSession session) {

        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday, lastday;
        // 获取当天
        Calendar cale = Calendar.getInstance();
        lastday = format.format(cale.getTime());
        // 获取当月的第一天
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());

        model.addAttribute("dtFrom", firstday);    // 当月的第一天
        model.addAttribute("dtTo", lastday);        // 当月的最后一天

        return "pc/ncov_report/ncov_report";
    }

    /**
     * 数据查询接口
     *
     * @param parameter
     * @param session
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "invs_ncov_report.do/load", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> loadDtClinicMedicalNcovListEvent(
            @RequestBody NcovParModel parameter,
            HttpSession session) throws ParseException {

        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("errcode", -1);
        ret.put("errMsg", "系统出错，请稍后再试！");

        // 日期check
        if (StringUtils.isBlank(parameter.getDtFrom()) ||
                StringUtils.isBlank(parameter.getDtTo())) {
            ret.put("errMsg", "没有输入查询日期！");
            return ret;
        }

        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            format.parse(parameter.getDtFrom());
            format.parse(parameter.getDtTo());
        } catch (Exception e) {
            ret.put("errMsg", "输日期格式必须为：yyyy-MM-dd");
            return ret;
        }

        SessionLoginData loginData = (SessionLoginData) session.getAttribute(SessionLoginData.KEY);
        // session中卫监所的基础信息传入service
        parameter.setInvsInstitutionId(loginData.getInstitution().getId());

        // 查询实施
        List<NocResModel> datalist = ncovReportService.searchNcovReportList(parameter);
//        try{
//            maInvsDrugService.selectBySpecification();
//        }
//        catch(Exception e){
//            e.printStackTrace();
//        }

        ret.put("datalist", datalist);

        ret.put("errcode", 0);
        ret.put("errMsg", "");
        return ret;
    }

    public void testSpecification(){

    }

    /**
     * 新冠患者 明细数据接口
     *
     * @param
     * @param session
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "invs_ncov_report.do/load_detail", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> loadNcovReportDetailEvent(
            @RequestParam Integer id,
            //NcovParModel _in,
            HttpSession session) throws ParseException {

        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("errcode", -1);
        ret.put("errMsg", "系统出错，请稍后再试！");

//        SessionLoginData loginData = (SessionLoginData) session.getAttribute(SessionLoginData.KEY);
//        _in.setInvsInstitutionId(loginData.getInstitution().getId());
//
//        // 访问权限check(防止异常调用)
//        if (!sewageService.checkAccessAuthority(loginData.getInstitution().getId(), _in.getInstitutionId())) {
//            ret.put("errMsg", "没有访问权限！");
//            return ret;
//        }
//
//        // 输入参数check
//        if (StringUtils.isBlank(_in.getId())) {
//            ret.put("errMsg", "系统出错，没有输入参数！");
//            return ret;
//        }

        // 查询实施
//        ret.put("data", ncovReportService.searchNcovReportDetail(_in.getInstitutionId(),_in.getDoctorId(),_in.getId()));
        ret.put("data", ncovReportService.searchNcovReportDetail("","",id.toString()));

        ret.put("errcode", 0);
        ret.put("errMsg", "");

        return ret;
    }

    /**
     * 根据诊所所在街道统计待观察患者数据并以饼状图形式展示
     *
     * @param
     * @param session
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "invs_ncov_report.do/statistics_patient_by_institution", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> statisticsPatientByInstitutionEvent(
            HttpSession session) throws ParseException {
        NcovParModel parameter=new NcovParModel();
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("errcode", -1);
        ret.put("errMsg", "系统出错，请稍后再试！");

        SessionLoginData loginData = (SessionLoginData) session.getAttribute(SessionLoginData.KEY);
        // session中卫监所的基础信息传入service
        parameter.setInvsInstitutionId(loginData.getInstitution().getId());
        parameter.setSuspectedResult("待观察");
        // 查询当前卫健下面所有诊所的待观察患者列表
        List<NocStatisticsResModel> datalist = ncovReportService.searchStatisticsPatientList(parameter);

        // 查询当前卫健下面所有待观察患者总数以及所有患者总数
        NocStatisticsResModel data = ncovReportService.searchStatisticsPatientAmount(parameter);
        //按区域诊所统计疑似冠状患者饼状图数据
        ret.put("datalist", datalist);

        ret.put("data", data);
        ret.put("errcode", 0);
        ret.put("errMsg", "");
        return ret;
    }

    /**
     * 统计当前卫健下所有待观察患者，所有患者汇总
     *
     * @param parameter
     * @param session
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "invs_ncov_report.do/statistics_patient_amount", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> statisticsPatientAmountEvent(
            @RequestBody NcovParModel parameter,
            HttpSession session) throws ParseException {

        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("errcode", -1);
        ret.put("errMsg", "系统出错，请稍后再试！");

        SessionLoginData loginData = (SessionLoginData) session.getAttribute(SessionLoginData.KEY);
        // session中卫监所的基础信息传入service
        parameter.setInvsInstitutionId(loginData.getInstitution().getId());

        // 查询当前卫健下面所有待观察患者总数以及所有患者总数
        NocStatisticsResModel data = ncovReportService.searchStatisticsPatientAmount(parameter);
        ret.put("data", data);

        ret.put("errcode", 0);
        ret.put("errMsg", "");
        return ret;
    }

}
